package com.tanhua.app.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.api.db.BlackListApi;
import com.tanhua.api.db.QuestionApi;
import com.tanhua.api.db.SettingsApi;
import com.tanhua.db.Question;
import com.tanhua.db.Settings;
import com.tanhua.db.User;
import com.tanhua.vo.PageBeanVo;
import com.tanhua.vo.SettingsVo;
import com.tanhua.vo.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
public class SettingsService {

    @Reference
    private QuestionApi questionApi;

    @Reference
    private SettingsApi settingsApi;

    @Reference
    private BlackListApi blackListApi;

    public ResponseEntity findSettingsVo() {
        //1.获取当前用户
        User user = UserHolder.getUser();
        SettingsVo settingsVo = new SettingsVo();
        //2.获取陌生人问题
        Question question = questionApi.findByUserId(user.getId());
        if (question != null) {
            settingsVo.setStrangerQuestion(question.getStrangerQuestion());
        }
        //3.获取通用设置信息
        Settings settings = settingsApi.findByUserId(user.getId());
        if (settings != null) {
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());

        }
        // 4、设置手机号
        settingsVo.setPhone(user.getPhone());
        // 5、返回vo对象
        return ResponseEntity.ok(settingsVo);


    }

    public ResponseEntity saveQuestion(String content) {
        //1.获取当前用户
        User user = UserHolder.getUser();
        //2.获取当前用户问题
        Question question = questionApi.findByUserId(user.getId());
        if (question == null) {
            Question question1 = new Question();
            question1.setUserId(user.getId());
            question1.setStrangerQuestion(content);
            questionApi.save(question1);
        } else {
            question.setUserId(user.getId());
            question.setStrangerQuestion(content);
            questionApi.update(question);

        }
        return ResponseEntity.ok(null);
    }

    public ResponseEntity saveSettings(Settings settings) {
        //1.获取当前用户
        User user = UserHolder.getUser();
        Long userId = user.getId();
        //2.获取当前用户设置
        Settings findSettings = settingsApi.findByUserId(userId);
        if (findSettings == null) {
            settings.setId(userId);
            settingsApi.save(settings);
        } else {
            findSettings.setGonggaoNotification(settings.getGonggaoNotification());
            findSettings.setLikeNotification(settings.getLikeNotification());
            findSettings.setPinglunNotification(settings.getPinglunNotification());
            settingsApi.update(findSettings);
        }
        return ResponseEntity.ok(null);

    }

    public ResponseEntity findBlackListPage(Integer page, Integer pageSize) {
        Long id = UserHolder.getUser().getId();

        IPage iPage = blackListApi.findBlackList(page, pageSize, id);
        PageBeanVo pageBeanVo = new PageBeanVo(page, pageSize, iPage.getTotal(), iPage.getRecords());

        return ResponseEntity.ok(pageBeanVo);
    }
    public ResponseEntity deleteBlack(Long blackUserId) {
        Long userId = UserHolder.getUser().getId();
        blackListApi.deleteBlack(userId, blackUserId);
        return ResponseEntity.ok(null);
    }
}
